package main

import (
	"fmt"
	"net"
)

// tcp服务器

func main() {
	fmt.Println("start server...")
	listener, err := net.Listen("tcp", "localhost:50000")
	if err != nil {
		fmt.Println("Error listening:", err)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("Error accepting:", err)
			return
		}
		go doServerStuff(conn)
	}
}

func doServerStuff(conn net.Conn) {
	for {
		buff := make([]byte, 512)
		if len, err := conn.Read(buff); err != nil {
			fmt.Println("Error reading:", err)
			return
		} else {
			fmt.Printf("Received data: %v", string(buff[:len]))
		}
	}
}
